package cn.chenc.framework.jpa.jdbc;


import cn.chenc.framework.core.util.AssertUtil;
import cn.chenc.framework.dynamic.datasource.DynamicDataSource;
import cn.chenc.framework.jpa.exception.RepositoryException;
import cn.chenc.framework.jpa.exception.ServiceException;
import cn.chenc.framework.jpa.model.PageInfo;
import cn.chenc.framework.jpa.util.SqlHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.jdbc.support.rowset.SqlRowSetMetaData;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 　@description: TODO
 * 　@author secret
 * 　@date 2021/7/21 22:14
 *
 */
@Component
public class BaseJdbcTemplateImpl implements BaseJdbcTemplate{

    @Autowired
    @Qualifier("multipleDataJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    public BaseJdbcTemplateImpl() {
    }

    @Override
    public List<Map<String, Object>> queryListMapBySql(String sql) throws Exception {
        AssertUtil.notNull(sql, "sql must not be null");

        try {
            return this.jdbcTemplate.queryForList(sql);
        } catch (IncorrectResultSizeDataAccessException var3) {
            return null;
        } catch (Exception var4) {
            throw new ServiceException("Problem invoking method, Cause:" + var4.getMessage(), var4);
        }
    }

    @Override
    public <T> List<T> queryListEntityBySql(String sql, Class<T> mappedClass) throws Exception {
        AssertUtil.notNull(sql, "sql must not be null");

        try {
            RowMapper<T> rowMapper = BeanPropertyRowMapper.newInstance(mappedClass);
            List<T> dd = this.jdbcTemplate.query(sql, rowMapper);
            return dd;
        } catch (Exception var5) {
            throw new ServiceException("Problem invoking method, Cause:" + var5.getMessage(), var5);
        }
    }

    @Override
    public PageInfo queryListMapBySql(String sql, PageInfo page) throws Exception {
        AssertUtil.notNull(sql, "sql must not be null");

        try {
            int pageNum = page.getPageNum();
            int pageSize = page.getPageSize();
            Map<String, Object> map = this.queryMapBySql(SqlHelper.queryCountSQL(sql));
            int totalCount = Integer.valueOf(String.valueOf(map.get("totalRow")));
            List<Map<String, Object>> list = this.queryListMapBySql(SqlHelper.buildDataSql(sql, page, DynamicDataSource.getDataSourceType()));
            PageInfo pageUtil = new PageInfo(pageNum, pageSize, totalCount);
            pageUtil.setCurrPageCount(list.size());
            pageUtil.setContent(list);
            return pageUtil;
        } catch (Exception var9) {
            throw new ServiceException("Problem invoking method, Cause:" + var9.getMessage(), var9);
        }
    }

    @Override
    public <T> PageInfo queryListEntityBySql(String sql, Class<T> mappedClass, PageInfo page) throws Exception {
        AssertUtil.notNull(sql, "sql must not be null");

        try {
            int pageNum = page.getPageNum();
            int pageSize = page.getPageSize();
            Map<String, Object> map = this.queryMapBySql(SqlHelper.queryCountSQL(sql));
            int totalCount = Integer.valueOf(String.valueOf(map.get("totalRow")));
            List<T> list = this.queryListEntityBySql(SqlHelper.buildDataSql(sql, page, DynamicDataSource.getDataSourceType()), mappedClass);
            PageInfo pageUtil = new PageInfo(pageNum, pageSize, totalCount);
            pageUtil.setCurrPageCount(list.size());
            pageUtil.setContent(list);
            return pageUtil;
        } catch (Exception var10) {
            throw new ServiceException("Problem invoking method, Cause:" + var10.getMessage(), var10);
        }
    }

    @Override
    public Map<String, Object> queryMapBySql(String sql) throws Exception {
        AssertUtil.notNull(sql, "sql must not be null");

        try {
            return this.jdbcTemplate.queryForMap(sql);
        } catch (IncorrectResultSizeDataAccessException var3) {
            return null;
        } catch (Exception var4) {
            var4.printStackTrace();
            throw new ServiceException("Problem invoking method, Cause:" + var4.getMessage(), var4);
        }
    }

    @Override
    public JdbcTemplate queryJdbcTemplate() throws Exception {
        return this.jdbcTemplate;
    }

    @Override
    public void execute(String sql) throws Exception {
        AssertUtil.notNull(sql, "sql must not be null");

        try {
            this.jdbcTemplate.execute(sql);
        } catch (Exception var3) {
            throw new ServiceException("Problem invoking method, Cause:" + var3.getMessage(), var3);
        }
    }

    @Override
    public <T> T queryEntityBySql(String sql, Class<T> mappedClass) throws Exception {
        AssertUtil.notNull(sql, "sql must not be null");

        try {
            RowMapper<T> rowMapper = BeanPropertyRowMapper.newInstance(mappedClass);
            return this.jdbcTemplate.queryForObject(sql, rowMapper);
        } catch (Exception var4) {
            throw new ServiceException("Problem invoking method, Cause:" + var4.getMessage(), var4);
        }
    }

    @Override
    public List<Map<String, String>> getColumnsAndTypeBySql(String sql) throws RepositoryException {
        ArrayList columnList = new ArrayList();

        try {
            SqlRowSet rowSet = this.jdbcTemplate.queryForRowSet(sql);
            SqlRowSetMetaData metaData = rowSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            Map<String, Integer> strMap = new HashMap();

            for(int i = 1; i <= columnCount; ++i) {
                Map<String, String> map = new HashMap();
                String columnName = metaData.getColumnName(i);
                if (strMap.get(columnName) == null) {
                    strMap.put(columnName, 1);
                } else {
                    int number = (Integer)strMap.get(columnName);
                    columnName = columnName + number;
                    ++number;
                    strMap.put(columnName, number);
                }

                map.put("columnName", columnName);
                map.put("columnType", String.valueOf(metaData.getColumnType(i)));
                columnList.add(map);
            }

            return columnList;
        } catch (Exception var11) {
            throw new RepositoryException(var11.getMessage(), var11);
        }
    }

    @Override
    public List<String> getColumnsBySql(String sql) throws RepositoryException {
        ArrayList columnList = new ArrayList();

        try {
            SqlRowSet rowSet = this.jdbcTemplate.queryForRowSet(sql);
            SqlRowSetMetaData metaData = rowSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            Map<String, Integer> strMap = new HashMap();

            for(int i = 0; i < columnCount; ++i) {
                String columnName = metaData.getColumnName(i + 1).toLowerCase();
                if (strMap.get(columnName) == null) {
                    strMap.put(columnName, 1);
                } else {
                    int number = (Integer)strMap.get(columnName);
                    columnName = columnName + number;
                    ++number;
                    strMap.put(columnName, number);
                }

                columnList.add(columnName);
            }

            return columnList;
        } catch (Exception var10) {
            throw new RepositoryException(var10.getMessage(), var10);
        }
    }

    @Override
    public PageInfo queryListMapNoTotalBySql(String sql, PageInfo page) throws Exception {
        AssertUtil.notNull(sql, "sql must not be null");

        try {
            int pageNum = page.getPageNum();
            int pageSize = page.getPageSize();
            List<Map<String, Object>> list = this.queryListMapBySql(SqlHelper.buildDataSql(sql, page, DynamicDataSource.getDataSourceType()));
            PageInfo pageUtil = new PageInfo(pageNum, pageSize, 0);
            pageUtil.setCurrPageCount(list.size());
            pageUtil.setContent(list);
            return pageUtil;
        } catch (Exception var7) {
            throw new ServiceException("Problem invoking method, Cause:" + var7.getMessage(), var7);
        }
    }
}
